package cn.itsource.service.impl;

import cn.itsource.config.anno.MyLog;
import cn.itsource.domain.BlogArticle;
import cn.itsource.domain.BlogArticleType;
import cn.itsource.mapper.ArticleMapper;
import cn.itsource.mapper.ArticleTypeMapper;
import cn.itsource.query.BlogArticleQuery;
import cn.itsource.service.BlogArticleService;
import cn.itsource.util.UserTools;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service //交给spring容器管理
public class BlogArticleServiceImpl implements BlogArticleService {

    /**
     * 注入mapper层
     */
    @Autowired
    private ArticleMapper articlemapper;
    @Autowired
    private ArticleTypeMapper typeMapper;
    @Override
    public PageInfo<BlogArticle> HtmlpageList(BlogArticleQuery query) {
        //1.使用分页插件  limit count(*)
        PageHelper.startPage(query.getPage(), query.getRows());
        //2.调用查询所有的方法，注意：要查询发布状态文章 selectByExample(example)
        Example example = new Example(BlogArticle.class);//封装查询条件的
        example.and().andEqualTo("articleState",query.getArticleState());
        List<BlogArticle> blogArticles = articlemapper.selectByExample(example);
        //循环给文章对象添加类型名称
        for (BlogArticle blogArticle : blogArticles) {
            //通过文章数据拿到类型主键，查类型
            BlogArticleType articleType = typeMapper.selectByPrimaryKey(blogArticle.getArticleType());
            blogArticle.setArticleTypeName(articleType.getTypeName());
        }
        //3.封装返回pageInfo对象
        return new PageInfo<>(blogArticles);
    }

    @Override
    public List<BlogArticle> findType(Long id) {
        List<BlogArticle> type = articlemapper.findType(id);
        return type;
    }

    @Override
    public PageInfo<BlogArticle> pageList(BlogArticleQuery blogArticleQuery) {

        //分页核心方法
        PageHelper.startPage(blogArticleQuery.getPage(), blogArticleQuery.getRows());

        //紧接着查询所有，中间不能写别的代码
        List<BlogArticle> blogArticles = articlemapper.pageList(blogArticleQuery);

        //封装成pageInfo
        PageInfo<BlogArticle> pageInfo = new PageInfo<>(blogArticles);

        //返回数据
        return pageInfo;
    }

    @Override
    public BlogArticle findOne(Long id) {
        //使用tk的方法查询一个
        return articlemapper.selectByPrimaryKey(id);
    }

    @Override
    public List<BlogArticle> findAll() {
        return articlemapper.selectAll();
    }

    @Override
    @MyLog(value = "文章列表")

    public void save(BlogArticle blogArticle) {
        //新增作者id
        blogArticle.setUserId(UserTools.getCurrentUserId());
        //只会新增有值的字段
        articlemapper.insertSelective(blogArticle);
    }

    @Override
    @MyLog(value = "文章列表")

    public void update(BlogArticle blogArticle) {
//        更新最后修改的时间
        blogArticle.setUpdateTime(new Date());
        //只会修改被设置值的字段
        articlemapper.updateByPrimaryKeySelective(blogArticle);
    }

    @Override
    @MyLog(value = "文章列表", isImportant = true)
    public void delete(Long id) {
        //根据主键id删除一个对象
        articlemapper.deleteByPrimaryKey(id);
    }

    @Override
    public void delete(String id) {
        articlemapper.deleteByPrimaryKey(id);
    }


}
